package com.flash.DayProcessing.controller;

import ch.qos.logback.classic.db.names.TableName;
import com.flash.BusinessData.entity.CashAccrued;
import com.flash.BusinessData.entity.securityAccrued;
import com.flash.BusinessData.service.CashAccruedService;
import com.flash.BusinessData.service.SecurityAccruedService;
import com.flash.DayProcessing.entity.BondsInterest;
import com.flash.DayProcessing.entity.CashInterest;
import com.flash.DayProcessing.entity.TowFees;
import com.flash.DayProcessing.service.AccrualOfIncomeService;
import com.flash.GlobalManagement.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.ManagedMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.flash.GlobalManagement.util.NumberUtil.getNumber;

@RestController
public class AccrualOfIncomeController {
    @Autowired
    AccrualOfIncomeService accrualOfIncomeService;

    @Autowired
    CashAccruedService cashAccruedService;

    @Autowired
    SecurityAccruedService securityAccruedService;

    @Autowired
    Dbutil dbutil;


    //收益计提的搜索现金利息
    @RequestMapping("selectCashInterest")
    private Map selectCashInterest(String statDate,int page,int limit){
        System.out.println("这是收益计提现金");
        System.out.println("现金日期"+statDate);
        //调用收益计提的现金利息搜索的方法
        List<CashInterest> cashInterests = accrualOfIncomeService.selectCashInterest(statDate, page, limit);
        int count = accrualOfIncomeService.selectCashInterestCount();
        Map map=new HashMap();
        map.put("code","");
        map.put("msg","");
        map.put("data",cashInterests);
        map.put("count",count);
        return map;
    }

    //收益计提搜索债券利息的服务类
    @RequestMapping("selectBondsInterest")
    private Map selectBondsInterest(String statDate,int page,int limit){
        System.out.println("这是收益计提债券利息");
        List<BondsInterest> bondsInterests = accrualOfIncomeService.selectBondsInterest(statDate, page, limit);
        int count = accrualOfIncomeService.selectBondsInterestCount();
        Map map=new HashMap();
        map.put("code","");
        map.put("msg","");
        map.put("data",bondsInterests);
        map.put("count",count);
        return map;
    }
    //收益计提两费利息的搜索
    @RequestMapping("selectTowFeesInterest")
    private Map selectTowFeesInterest(String statDate,int page,int limit){
        System.out.println("这是收益计提两费利息");
        List<TowFees> towFeesList = accrualOfIncomeService.selectTowFeesInterest(statDate, page, limit);
        int count = accrualOfIncomeService.selectTowFeesInterestCount();
        Map map=new HashMap();
        map.put("code","");
        map.put("msg","");
        map.put("data",towFeesList);
        map.put("count",count);
        return map;
    }



    //收益计提的现金的统计
    @RequestMapping("StatisticsCash")
    private int StatisticsCash(String cash, HttpServletRequest request){
        System.out.println("进收益计提现金统计");
        //从session中拿基金id
        HttpSession session = request.getSession();
        String fundId = (String) session.getAttribute("fundId");
        int i=0;
        System.out.println("现金统计cash="+cash);
        //通过调用SysUtil.jsonToArrayList(cash, CashInterest.class);工具类得到CashInterest类型的集合
        //CashInterest 收益计提现金的实体类
        List<CashInterest> cashInterestList = SysUtil.jsonToArrayList(cash, CashInterest.class);
        System.out.println(cashInterestList+"收益计提现金实体类");
        //现金应收应付
        CashAccrued cashAccrued=new CashAccrued();
        //遍历循环
        for (CashInterest cashInterest: cashInterestList) {
            System.out.println("遍历循环进来了");
            //现金应收应付Id
//          1String maxId = dbutil.getMaxId(TableIdUtil.CAID, TableNameUtil.CA, TableTimeUtil.CATime);
            String maxId = getNumber(TableIdUtil.CAID, TableNameUtil.CA, "CA");
            System.out.println("这是现金统计最大id"+maxId);
            cashAccrued.setCashAccruedId(maxId);
            //基金id
            cashAccrued.setFundId(fundId);
            //业务日期
            cashAccrued.setBusinessTime(cashInterest.getDateCashStock());
            //现金账户
            cashAccrued.setCashId(cashInterest.getCashId());
            //业务类型
            cashAccrued.setBusinessType(3);
            //资金流向
            cashAccrued.setFundFlow(1);
            //.setMoney 放入利息  getInterest get利息
            cashAccrued.setMoney(cashInterest.getInterest());
            //调用现金应收应付的删除方法
             cashAccruedService.deleteCashAccruedNew(cashAccrued);
            System.out.println(i);
            //调用现金应收应付的增加方法
            i=cashAccruedService.insertCashAccrued(cashAccrued);
            System.out.println("这是现金利息统计先删后增"+i);

        }
        return i;
    }

    //收益计提债券的统计
    @RequestMapping("StatisticalBond")
    public int StatisticalBond(String Securities,String accountId,HttpServletRequest request){
        int i=0;
        System.out.println("进收益计提债券的统计");
        System.out.println("Securities="+Securities);
        System.out.println(accountId);
        //从session里拿基金id和账号id
        HashMap map=new HashMap();
        HttpSession session = request.getSession();
        String fundId = (String) session.getAttribute("fundId");
        String cashId = (String) session.getAttribute("cashId");
        //BondsInterest 收益计提债券的实体类
        List<BondsInterest> bondsInterestList = SysUtil.jsonToArrayList(Securities, BondsInterest.class);
        //遍历循环BondsInterest
        for (BondsInterest bondsInterest:bondsInterestList) {
            //证券应收应付实体类
            securityAccrued securityAccrued=new securityAccrued();
            //证券应收应付Id
            String sa = getNumber(TableIdUtil.SAID, TableNameUtil.SA, "SA");
            securityAccrued.setSecurityAccruedId(sa);
            //业务日期
            securityAccrued.setBusinessTime(bondsInterest.getDateStock());
            //基金id
            securityAccrued.setFundId(fundId);
            //银行账号id
            securityAccrued.setCashId(cashId);
            //证券编号
            securityAccrued.setSecurityId(bondsInterest.getSecurityId());
            //业务类型
            securityAccrued.setBusinessType(3);
            //资金流向
            securityAccrued.setFundFlow(1);
            //金额
            securityAccrued.setMoney(bondsInterest.getInterest());
            System.out.println("securityAccrued="+securityAccrued);
            //调用证券应收应付的删除方法
            securityAccruedService.deleteSecurityAccruedByPojo(securityAccrued);
            //调用证券应收应付的增加方法
            i=securityAccruedService.SecurityAccruedInsert(securityAccrued);

        }
        return i;
    }
    //收益计提两费的统计
    @RequestMapping("/StatisticalTowFees")
    public int StatisticalTowFees(String TwoFees,String statDate,HttpServletRequest request){
        System.out.println("两费TwoFees"+TwoFees);
        System.out.println("两费的最大值"+statDate);
        System.out.println("进收益计提两费的统计");
        int i=0;
        //通过调用SysUtil.jsonToArrayList(TwoFees, com.flash.DayProcessing.entity.TowFees.class);工具类得到TwoFees类型的集合
        //TwoFees 收益计提两费的实体类
        List<TowFees> towFeesList = SysUtil.jsonToArrayList(TwoFees, com.flash.DayProcessing.entity.TowFees.class);
        for (TowFees towFees:towFeesList) {
            //从session里拿fundId
            HttpSession session = request.getSession();
            String fundId = (String) session.getAttribute("fundId");
            System.out.println("基金id"+fundId);
            //现金应收应付实体类
            CashAccrued cashAccrued=new CashAccrued();
            //现金应收应付Id
            System.out.println("statDate====="+statDate);
//            String maxId = dbutil.getMaxId3(TableIdUtil.CAID, TableNameUtil.CA, TableTimeUtil.CATime,towFees.getStatisticsDate());
//            System.out.println("statDate111====="+maxId);
//            System.out.println("这是两费统计最大id"+maxId);
            String maxId = getNumber(TableIdUtil.CAID, TableNameUtil.CA, "CA");
            cashAccrued.setCashAccruedId(maxId);
            //基金id
            cashAccrued.setFundId(fundId);
            //业务日期
            cashAccrued.setBusinessTime(towFees.getDateCashStock());
            //现金账户
            cashAccrued.setCashId(towFees.getCashId());
            //业务类型
            cashAccrued.setBusinessType(1);
            //资金流向
            cashAccrued.setFundFlow(1);
            //.setMoney 放入利息  getInterest get管理人利息
            cashAccrued.setMoney(towFees.getTrusteeInterest());
            System.out.println("cashAccrued="+cashAccrued);
            //调用现金应收应付的删除方法
            cashAccruedService.deleteCashAccruedNew(cashAccrued);
            System.out.println("管理人的添加");
            System.out.println("日期======="+cashAccrued.getBusinessTime());
            //调用现金应收应付的增加方法
            cashAccruedService.insertCashAccrued(cashAccrued);

            //.setMoney 放入利息  getInterest get托管人利息
            //业务类型
            cashAccrued.setBusinessType(2);
            //.setMoney 放入利息  getInterest get托管人利息
            cashAccrued.setMoney(towFees.getCustodianInterest());
            String number1 = getNumber("CashAccruedId", "CashAccrued", "CA");
            cashAccrued.setCashAccruedId(number1);
            //调用现金应收应付的删除方法
            cashAccruedService.deleteCashAccruedNew(cashAccrued);
            System.out.println("托管人的添加");
            //调用现金应收应付的增加方法
            i=cashAccruedService.insertCashAccrued(cashAccrued);
        }
        return i;
    }

}
